import sys
import asyncio
import time

import zmq
import zmq.asyncio

if sys.platform == "win32":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


async def publisher():
    # 创建一个ZMQ上下文和一个PUB套接字
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind('tcp://127.0.0.1:5555')  # 绑定到特定端口

    print("Publisher started, sending messages...")
    await asyncio.sleep(1)  # 等待订阅者连接

    while True:
        # 发布消息
        message = "Hello, World!  2"
        socket.send_string(f"topic {message}")
        print(f"Sent: topic {message}")
        time.sleep(1)


if __name__ == "__main__":
    asyncio.run(publisher())
